package com.appkubes.mall.admin.application.order.order.controller;


import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.order.order.entity.dto.GetOrderDetailDTO;
import com.appkubes.mall.admin.application.order.order.entity.vo.OrderSearchVO;
import com.appkubes.mall.admin.application.order.order.entity.vo.OrderShipVO;
import com.appkubes.mall.admin.application.order.order.service.OrderService;
import com.appkubes.mall.core.controller.BaseMallOrderController;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.core.entity.MallOrderGoods;
import com.appkubes.mall.core.service.BaseMallOrderGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Api(tags = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseMallOrderController {
    @Resource
    OrderService orderService;
    @Resource
    BaseMallOrderGoodsService baseMallOrderGoodsService;

    @ApiOperation("获得订单")
    @PostMapping("/search")
    public Response search(@ParamValid OrderSearchVO orderSearchVO) {
        MallOrder mallOrder = new MallOrder();
        BeanUtils.copyProperties(orderSearchVO, mallOrder);
        if ("completed".equalsIgnoreCase(orderSearchVO.getStatus())) {
            mallOrder.setStatus(null);
        }

        //条件查询器
        QueryWrapper<MallOrder> queryEntity = new QueryWrapper<>();
        queryEntity.setEntity(mallOrder);

        if (StringUtils.isEmpty(mallOrder.getStatus())) {
            queryEntity.in("status", "paySuccess", "sending", "using", "completed");
        }
        if ("completed".equalsIgnoreCase(orderSearchVO.getStatus())) {
            queryEntity.in("status", "using", "completed");
        }
        //复杂条件
        if (orderSearchVO.getStartDate() != null && orderSearchVO.getStartDate() > 0) {
            queryEntity.ge("create_time", orderSearchVO.getStartDate());
        }
        if (orderSearchVO.getEndDate() != null && orderSearchVO.getEndDate() > 0) {
            queryEntity.lt("create_time", orderSearchVO.getEndDate());
        }
        queryEntity.orderByDesc("create_time");
        IPage page = new Page(orderSearchVO.getPage(), orderSearchVO.getSize());
        IPage<MallOrder> pageResult = orderService.getBaseMapper().selectPage(page, queryEntity);
        return Response.success(pageResult);
    }

    @ApiOperation("订单详细")
    @GetMapping("/get/{orderKey}")
    public Response get(@PathVariable("orderKey") String orderKey) {
        GetOrderDetailDTO orderDetailDTO = new GetOrderDetailDTO();

        MallOrder mallOrder = orderService.getById(orderKey);

        MallOrderGoods queryEntity = new MallOrderGoods();

        queryEntity.setOrderKey(orderKey);
        List<MallOrderGoods> orderGoods = baseMallOrderGoodsService.selectListByEntity(queryEntity);

        orderDetailDTO.setOrder(mallOrder);
        orderDetailDTO.setOrderGoods(orderGoods);

        return Response.success(orderDetailDTO);
    }

    @ApiOperation("订单发货")
    @PostMapping("/ship")
    public Response ship(@ParamValid OrderShipVO orderShipVO) {
        Boolean flag = orderService.ship(orderShipVO);
        return Response.success(flag);
    }

    @ApiOperation("订单发货")
    @PostMapping("/addImei")
    public Response addImei(@ParamValid OrderShipVO orderShipVO) {
        Boolean flag = orderService.addImei(orderShipVO);
        return Response.success(flag);
    }

    @ApiOperation("获取订单商品规格及配件")
    @GetMapping("/getGoodsAttrAndAssociate/{orderKey}")
    public Response getGoodsAttrAndAssociate(@PathVariable("orderKey") String orderKey){
        MallOrderGoods queryEntity = new MallOrderGoods();
        queryEntity.setOrderKey(orderKey);
        queryEntity.setType(1);
        MallOrderGoods mallOrderGoods = this.baseMallOrderGoodsService.getOneByEntity(queryEntity);
        HashMap<String,Object> data = new HashMap<>();
        data.put("attr", StringUtils.isEmpty(mallOrderGoods.getAttrNames())?"":mallOrderGoods.getAttrNames());
        return Response.success(data);
    }
}
